package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.supervision.convert.SpAuditResultConvert;
import com.engine4cloud.tdf.supervision.dto.create.SpAuditActionResultCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.SpAuditResultQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.SpAuditResultDTO;
import com.engine4cloud.tdf.supervision.entity.SpAuditResultEntity;
import com.engine4cloud.tdf.supervision.mapper.SpAuditResultMapper;
import com.engine4cloud.tdf.supervision.service.SpAuditMessageService;
import com.engine4cloud.tdf.supervision.service.SpAuditResultService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 审批活动结果表
 *
 * @author wenjie
 * @date 2024-04-08 14:34:45
 */
@Service
public class SpAuditResultServiceImpl extends ServiceImpl<SpAuditResultMapper, SpAuditResultEntity> implements SpAuditResultService {
    @Resource
    private SpAuditMessageService auditMessageService;

    @Override
    public Boolean create(SpAuditActionResultCreateDTO dto) {
        super.save(SpAuditResultConvert.INSTANCE.create(dto));

        auditMessageService.sendMessageToLateral(dto.getBusinessType(), dto.getStatus());
        return true;
    }

    @Override
    public List<SpAuditResultDTO> detailList(SpAuditResultQueryDTO dto) {
        return super.list(wrapper(dto)).stream().map(SpAuditResultConvert.INSTANCE::convert).toList();
    }


    private LambdaQueryWrapper<SpAuditResultEntity> wrapper(SpAuditResultQueryDTO query) {
        LambdaQueryWrapper<SpAuditResultEntity> wrapper = Queries.lambda();
        Queries.accept(query.getRybh(), v -> wrapper.eq(SpAuditResultEntity::getRybh, v));
        Queries.accept(query.getLcbh(), v -> wrapper.eq(SpAuditResultEntity::getLcbh, v));
        Queries.accept(query.getBusinessType(), v -> wrapper.eq(SpAuditResultEntity::getYwlx, v));
        return wrapper;
    }
}